package com.alibaba.shopping_order_customer_api.controller;

import com.alibaba.shopping_common.pojo.CartGoods;
import com.alibaba.shopping_common.pojo.Orders;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.CartService;
import com.alibaba.shopping_common.service.OrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName OrderController
 * @Description 订单控制器
 * @Author Leo
 * @Date 2024/12/19 09:44
 */
@RestController
@RequestMapping("/user/orders")
public class OrderController {

    @DubboReference
    private OrderService orderService;

    @DubboReference
    private CartService cartService;

    @PostMapping("/add")
    public BaseResult<Orders>add(@RequestBody Orders orders,@RequestParam(name = "id",
            required = false, defaultValue = "29") Long id){
        // 保存订单
        orders.setUserId(id);
        Orders orders1 = orderService.buildOrder(orders);
        // 清空购物车中的商品数据
        List<CartGoods> cartGoods = orders.getCartGoods();
        for (CartGoods cartGood : cartGoods) {
            cartService.deleteCartOption(id,cartGood.getGoodId());
        }
        return BaseResult.OK(orders1);
    }

    /**
     * 根据ID查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Orders>findByID(String id){
        Orders orders = orderService.findById(id);
        return BaseResult.OK(orders);
    }

    /**
     * 查询用户所有的订单
     * @param status 订单状态：1.未付款 2.已付款 3. 未发货 4.已发货 5.交易成功 6.交易关闭 7.待评价，传入空 值代表查询所有
     * @param id
     * @return
     */
    @GetMapping("/findUserOrders")
    public BaseResult<List<Orders>>findUserOrders(Integer status,@RequestParam(name = "id",
            required = false, defaultValue = "29") Long id){
        List<Orders> userOrders = orderService.findUserOrders(id, status);
        return BaseResult.OK(userOrders);
    }

}




